package com.wnhz.work.yz.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.LibraryActivity;
import com.wnhz.work.yz.activity.WenKuHtmlActivity;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.ArticleBean;
import com.wnhz.work.yz.bean.LibraryBean2;
import com.wnhz.work.yz.utils.DisplayUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by d on 2017/1/5.
 */

public class FragmentLibrary extends BaseFragment {
    private static final String TAG = "FragmentLibrary";

    public static FragmentLibrary newInstance() {
        return new FragmentLibrary();
    }

    private RecyclerView recyclerView;
    private BaseRVAdapter<ArticleBean> adapter;
    private List<ArticleBean> mList = new ArrayList<>();

    private View head_view;
    private RecyclerView head_recyclerView;
    private BaseRVAdapter<LibraryBean2> headMolderBaseRVAdapter;
    private List<LibraryBean2> headMolderList = new ArrayList<>();

    private List<LibraryBean2> libraryList;
    private LibraryActivity activity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = (LibraryActivity) getActivity();
        return inflater.inflate(R.layout.fragment_library, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initHradView();

        recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new BaseRVAdapter<ArticleBean>(getContext(), mList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_library;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                ImageView img = holder.getView(R.id.img);
                x.image().bind(img, mList.get(position).getImg());
                holder.setTextView(R.id.title_tv, mList.get(position).getTitle());
                holder.setTextView(R.id.tv_time, mList.get(position).getPublishTime());
                holder.setTextView(R.id.tv_numbers, mList.get(position).getReadnums());
                holder.getView(R.id.line).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(activity, WenKuHtmlActivity.class);
                        intent.putExtra("html", mList.get(position).getUrl());
                        startActivity(intent);
                    }
                });
            }
        };
        recyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayout.HORIZONTAL
                , getResources().getDimensionPixelSize(R.dimen.line_h), getResources().getColor(R.color.line_color)));
        adapter.addHeaderView(head_view);
        recyclerView.setAdapter(adapter);
        upload(0);
        slectInt(0);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void upload(int s) {
        activity.showDialog();
        Log.i(TAG, "onSuccess: cateId111==  " + headMolderList.get(s).getCateId());
        Map<String, Object> map = new HashMap<>();
        map.put("cateId", headMolderList.get(s).getCateId());
//        map.put("cateId","7");
        XUtil.Post(Url.WENKU_GETARTICLELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: cateId111==  " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        mList.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            ArticleBean bean = new ArticleBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), ArticleBean.class);
                            mList.add(bean);
                        }
                    }


                    adapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                Log.i(TAG, "onFinished: cateId111==  "+mList.size());
                if (mList.size() == 0) {
                    recyclerView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                }
                activity.closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: cateId111==  ");
                activity.closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void initHradView() {
        head_view = LayoutInflater.from(getContext()).inflate(R.layout.include_card_head, null);
        head_recyclerView = (RecyclerView) head_view.findViewById(R.id.recyclerView);
        head_recyclerView.setBackgroundColor(getResources().getColor(R.color.main_background));
        head_recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        headMolderBaseRVAdapter = new BaseRVAdapter<LibraryBean2>(getContext(), headMolderList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_library_head;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                TextView textView = holder.getTextView(R.id.text);
                int padd = DisplayUtil.dp2px(mContext, 6);
                switch (position % 3) {
                    case 0:
                        holder.itemView.setPadding(padd * 2, padd, padd, padd);
                        break;
                    case 1:
                        holder.itemView.setPadding(padd, padd, padd, padd);
                        break;
                    case 2:
                        holder.itemView.setPadding(padd, padd, padd * 2, padd);
                        break;
                }
                Log.e(TAG, "onBind: " + mList.get(position).isSelector());
                textView.setSelected(mList.get(position).isSelector());
                textView.setText(mList.get(position).getTitle());
                if (textView.isSelected()) {
                    textView.setTextColor(getResources().getColor(R.color.white));
                } else {
                    textView.setTextColor(getResources().getColor(R.color.text_two));
                }

            }
        };
        headMolderBaseRVAdapter.addItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                upload(position);
                slectInt(position);

                headMolderBaseRVAdapter.notifyDataSetChanged();
            }
        });
        head_recyclerView.setAdapter(headMolderBaseRVAdapter);
    }

    private void slectInt(int position) {
        for (int i = 0; i < headMolderList.size(); i++) {
            if (position == i) {
                headMolderList.get(i).setSelector(true);

            } else {
                headMolderList.get(i).setSelector(false);
            }
        }
    }

    public void setData(List<LibraryBean2> libraryList) {
        this.headMolderList = libraryList;

    }
}
